package com.viphuli.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.daimajia.slider.library.SliderLayout;
import com.loopj.android.http.RequestParams;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.R;
import com.viphuli.common.MyApp;
import com.viphuli.common.MyPageHelper;
import com.viphuli.http.ApiRequest;
import com.viphuli.http.bean.page.HomeRoomServiceItemPage;
import com.viphuli.http.handler.MyBaseHttpResponseHandler.HandlerType;

/**
 * 上门体检
 * 
 * @author li.li
 *
 */
public class HomeServiceInfoFragment extends BaseProgressFragment {

	@Bind(R.id.id_main_home_service_item_info_slider)
	protected SliderLayout slider;

	@Bind(R.id.id_main_home_service_item_info_tabhost)
	protected FragmentTabHost tabHost;

	private HomeRoomServiceItemPage itemPage;

	private GestureDetector gestureDetector;

	private InfoTabEnum[] tabs = InfoTabEnum.values();

	private int serviceType;
	private int serviceItemId;
	private String serviceItemName;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_home_service_info;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	protected void initParams() {
		Bundle args = getArguments();
		serviceItemId = args.getInt("service_item_id");
		serviceItemName = args.getString("service_item_name");
		serviceType = args.getInt("service_type");

	}

	@Override
	protected void initView() {

		GestureListener gListener = new GestureListener(getActivity(), slider);
		gestureDetector = new GestureDetector(gListener);

		//顶部轮转图
		slider.setPresetTransformer(SliderLayout.Transformer.Default);
		slider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
//		slider.setCustomAnimation(new DescriptionAnimation());

		//初始化tabs
		tabHost.setup(getActivity(), getActivity().getSupportFragmentManager(), R.id.id_main_home_service_item_info_content);

		for (int i = 0; i < tabs.length; i++) {

			InfoTabEnum tab = tabs[i];
			// 给每个Tab按钮设置图标、文字和内容
			String name = tab.getName();
			TabSpec tabSpec = tabHost.newTabSpec(name).setIndicator(getTabItemView(tab));
			// 将Tab按钮添加进Tab选项卡中
			tabHost.addTab(tabSpec, tab.getFrag(), null);
			// 设置Tab按钮的背景
			tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.bg_main_bottom_tab);

		}

	}

	@Override
	protected void initData() {

		initRequest(HandlerType.loading);

	}

	@Override
	protected void initRequest(HandlerType handlerType) {
		//网络请求
		RequestParams params = new RequestParams();
		params.put("service_item_id", serviceItemId);
		ApiRequest.homeRoomServiceInfo.request(this, params, handlerType);
	}

	@OnClick(R.id.id_main_home_service_item_info_order_btn)
	public void goToOrder() {

		PurchaseOrderFillFragment.go(getActivity(), serviceItemId, serviceType, serviceItemName);

	}

	/**
	*
	* 给每个Tab按钮设置图标和文字
	*/
	private View getTabItemView(InfoTabEnum tab) {
		View view = ViewUtils.inflateView(getActivity(), R.layout.tab_home_service_info_item_view);
		TextView textView = (TextView) view.findViewById(R.id.id_main_home_service_item_info_tab_text);
		String name = tab.getName();
		textView.setText(name);

		return view;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
		if (slider != null)
			slider.startAutoCycle();
	}

	@Override
	public void onStop() {
		super.onStop();

		if (slider != null)
			slider.stopAutoCycle();
	}

	public SliderLayout getSlider() {
		return slider;
	}

	public FragmentTabHost getTabHost() {
		return tabHost;
	}

	public void setItemPage(HomeRoomServiceItemPage itemPage) {
		this.itemPage = itemPage;
	}

	public HomeRoomServiceItemPage getItemPage() {
		return itemPage;
	}

	public int getServiceItemId() {
		return serviceItemId;
	}

	/**
	 * 
	 * 标签切换
	 * 
	 */
	public static enum InfoTabEnum {
		introduce(MyApp.getInstance().getString(R.string.str_main_home_service_item_info_introduce), HomeServiceInfoIntroduceFragment.class), //
		comment(MyApp.getInstance().getString(R.string.str_main_home_service_item_info_comment), HomeServiceInfoCommentFragment.class), //
		needKnow(MyApp.getInstance().getString(R.string.str_main_home_service_item_info_need_know), HomeServiceInfoNeedKnowFragment.class), //
		;

		private InfoTabEnum(String name, Class<? extends Fragment> frag) {
			this.name = name;
			this.frag = frag;
		}

		/** 选修卡文字 */
		private String name;
		/** 选修卡展示类 */
		private Class<? extends Fragment> frag;

		public String getName() {
			return name;
		}

		public Class<? extends Fragment> getFrag() {
			return frag;
		}

	}

	/**
	 * 设置滑动监听
	 * @param scrollView
	 */
	public void setScrollTouchEvent(View scrollView) {

//		scrollView.setOnTouchListener(new OnTouchListener() {
//
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//
//				gestureDetector.onTouchEvent(event);
//
//				return false;
//			}
//		});
	}

	/**
	 * 进入服务详情
	 * @param act
	 * @param serviceItemId
	 * @param serviceType
	 * @param name
	 */
	public static void go(Activity act, int serviceItemId, int serviceType, String serviceItemName) {
		Bundle args = new Bundle();
		args.putInt("service_item_id", serviceItemId);
		args.putString("service_item_name", serviceItemName);
		args.putInt("service_type", serviceType);
		MyPageHelper.homeServiceInfo.showMyPage(act, args, serviceItemName);
	}

	/**
	 * 手势识别 
	 *
	 */
	private static final class GestureListener extends SimpleOnGestureListener {
		private Activity act;
		private View view;
//		private Animation mShowAction;
//		private Animation mHiddenAction;

		public GestureListener(Activity act, View view) {
			this.act = act;
			this.view = view;

//			mShowAction = AnimationUtils.loadAnimation(act, R.anim.push_up_in);
//			mHiddenAction = AnimationUtils.loadAnimation(act, R.anim.push_up_out);
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
			float distanceScreenY = e2.getRawY() - e1.getRawY();

			if (distanceScreenY < 0 && view != null && view.getVisibility() == View.VISIBLE) {

				view.setVisibility(View.GONE);
//				view.startAnimation(mHiddenAction);

			} else if (distanceScreenY > 0 && view != null && view.getVisibility() == View.GONE) {

				view.setVisibility(View.VISIBLE);
//				view.startAnimation(mShowAction);
			}

			return super.onScroll(e1, e2, distanceX, distanceY);
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
			return super.onFling(e1, e2, velocityX, velocityY);
		}
	}

}
